package com.ruoyi.bookSys.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.bookSys.domain.BookBorrow;
import com.ruoyi.bookSys.mapper.BookInfoMapper;
import com.ruoyi.bookSys.domain.BookInfo;
import com.ruoyi.bookSys.service.IBookInfoService;

/**
 * 图书信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-14
 */
@Service
public class BookInfoServiceImpl implements IBookInfoService 
{
    @Autowired
    private BookInfoMapper bookInfoMapper;

    /**
     * 查询图书信息
     * 
     * @param bookId 图书信息主键
     * @return 图书信息
     */
    @Override
    public BookInfo selectBookInfoByBookId(Long bookId)
    {
        return bookInfoMapper.selectBookInfoByBookId(bookId);
    }

    /**
     * 查询图书信息列表
     * 
     * @param bookInfo 图书信息
     * @return 图书信息
     */
    @Override
    public List<BookInfo> selectBookInfoList(BookInfo bookInfo)
    {
        return bookInfoMapper.selectBookInfoList(bookInfo);
    }

    /**
     * 新增图书信息
     * 
     * @param bookInfo 图书信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBookInfo(BookInfo bookInfo)
    {
        bookInfo.setCreateTime(DateUtils.getNowDate());
        int rows = bookInfoMapper.insertBookInfo(bookInfo);
        insertBookBorrow(bookInfo);
        return rows;
    }

    /**
     * 修改图书信息
     * 
     * @param bookInfo 图书信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBookInfo(BookInfo bookInfo)
    {
        bookInfo.setUpdateTime(DateUtils.getNowDate());
        bookInfoMapper.deleteBookBorrowByBookId(bookInfo.getBookId());
        insertBookBorrow(bookInfo);
        return bookInfoMapper.updateBookInfo(bookInfo);
    }

    /**
     * 批量删除图书信息
     * 
     * @param bookIds 需要删除的图书信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookInfoByBookIds(Long[] bookIds)
    {
        bookInfoMapper.deleteBookBorrowByBookIds(bookIds);
        return bookInfoMapper.deleteBookInfoByBookIds(bookIds);
    }

    /**
     * 删除图书信息信息
     * 
     * @param bookId 图书信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookInfoByBookId(Long bookId)
    {
        bookInfoMapper.deleteBookBorrowByBookId(bookId);
        return bookInfoMapper.deleteBookInfoByBookId(bookId);
    }

    /**
     * 新增图书借阅记录信息
     * 
     * @param bookInfo 图书信息对象
     */
    public void insertBookBorrow(BookInfo bookInfo)
    {
        List<BookBorrow> bookBorrowList = bookInfo.getBookBorrowList();
        Long bookId = bookInfo.getBookId();
        if (StringUtils.isNotNull(bookBorrowList))
        {
            List<BookBorrow> list = new ArrayList<BookBorrow>();
            for (BookBorrow bookBorrow : bookBorrowList)
            {
                bookBorrow.setBookId(bookId);
                list.add(bookBorrow);
            }
            if (list.size() > 0)
            {
                bookInfoMapper.batchBookBorrow(list);
            }
        }
    }
}
